package edu.aysf.controller;

import edu.aysf.entity.UserEntity;
import edu.aysf.service.UserService;
import edu.aysf.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    // 登录验证
    @RequestMapping("/login")
    public List<Integer> selectUserByNamePwd(@RequestBody Map<String,Object> map) {
        String name = map.get("name").toString();
        String password = map.get("password").toString();
        List<Integer> list = new ArrayList<>();
        list.add(userService.selectUserByNamePwd(name,password));
        return list;
    }

    @RequestMapping("/select/name")
    public List<UserEntity> selectUserName(@RequestBody Map<String,Object> map){
        String name =  map.get("name").toString();
        return userService.selectUserName(name);
    }


    @RequestMapping("/select/id")
    public List<UserEntity> selectUserById(@RequestBody Map<String,Object> map){
        int id = Integer.parseInt(map.get("id").toString());
//        int id = (int)map.get("id");
        return userService.selectUserById(id);
    }

    @RequestMapping("/select/all")
    public List<UserEntity> selectUser() {
        return userService.selectUser();
    }

    @RequestMapping("/create")
    public List<Integer> createUser(@RequestBody Map<String,Object> map) {
        String name = map.get("name").toString();
        String password = map.get("password").toString();
        int age = (int)map.get("age");
        String sex = map.get("sex").toString();
        String identity = map.get("identity").toString();
        String isadmin = map.get("isadmin").toString();
        String isdelete = map.get("isdelete").toString();

        List<Integer> list = new ArrayList<>();
        list.add(userService.createUser(name,password,age,sex,identity,isadmin,isdelete));
        return list;
    }

    @RequestMapping("/update")
    // 更新用户
    public List<Integer> updateUser(@RequestBody Map<String,Object> map) {
        int id = (int)map.get("id");
        String name = map.get("name").toString();
        String password = map.get("password").toString();
        int age = (int)map.get("age");
        String sex = map.get("password").toString();
        String identity = map.get("name").toString();
        String isadmin = map.get("password").toString();
        String isdelete = "0";

        List<Integer> list = new ArrayList<>();
        list.add(userService.updateUser(id,name,password,age,sex,identity,isadmin,isdelete));
        return list;
    }

    @RequestMapping("/delete")
    // 逻辑删除用户，更新isdelete字段
    public void deleteUser(@RequestBody Map<String,Object> map) {
        int id = (int)map.get("id");
        userService.deleteUser(id);
//        List<Integer> list = new ArrayList<>();
//        list.add(userService.deleteUser(id));
//        return list;
    }

}
